package cn.ze.yygh.hosp.controller;

import cn.ze.yygh.common.result.Result;
import cn.ze.yygh.common.utils.MD5;
import cn.ze.yygh.hosp.service.HospitalSetService;
import cn.ze.yygh.model.hosp.HospitalSet;
import cn.ze.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;


//http://localhost:8201/admin/hosp/hospitalSet
@Api(tags = "医院设置管理")
@RequestMapping("/admin/hosp/hospitalSet")
@RestController
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //1. 查询医院设置表的所有信息
    //http://localhost:8201/admin/hosp/hospitalSet/findAll
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("findAll")
    public Result findAllHospital(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //2.删除医院设置 逻辑删除
    @ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping("{id}")
    //@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public Result removeHospSet(@PathVariable("id") Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag) return Result.ok();
        else return Result.fail();
    }


    /**
     *   利用医院名称(模糊)   医院编号查询
     *   显示查到的列表
     *   首页  上一页 下一页 尾页
     */

    //3.条件查询待分页
    @ApiOperation(value = "条件查询待分页")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHpspSet(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) HospitalQueryVo hospitalQueryVo){
        //创建page对象,传递当前页 每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalQueryVo.getHosname();
        String hoscode = hospitalQueryVo.getHoscode();
        if (StringUtils.hasLength(hosname)) wrapper.like("hosname",hosname);
        if (StringUtils.hasLength(hoscode)) wrapper.eq("hoscode",hoscode);
        //调用方式实现分页查询
        Page<HospitalSet> page1 = hospitalSetService.page(page, wrapper);

        //**********



        return Result.ok(page1);
    }

    //4.添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public  Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 1是可使用 0是不可使用(删除)
        hospitalSet.setStatus(1);
        //设置签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) return Result.ok();
        else return Result.fail();

    }

    //5.根据ID获取医院设置
    @ApiOperation(value = "根据ID获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6.修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) return Result.ok();
        else return Result.fail();
    }

    //7.批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //8.医院设置的锁定和解锁接口  只有state = 1 才能操作
    @ApiOperation(value = "医院设置的锁定和解锁接口")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status")Integer status){
        // 先查后改
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //9.发送签名密钥
    @PutMapping("sendKey/{id}/")
    public Result sendKey(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信

        return Result.ok();
    }
}
